package com.huaguoguo.cyg.spider.pipeline;

import cn.hutool.core.bean.BeanUtil;
import com.huaguoguo.cyg.entity.tl.dto.TlGangDTO;
import com.huaguoguo.cyg.entity.tl.dto.TlPlayerDTO;
import com.huaguoguo.cyg.entity.tl.dto.TlTrumpetDTO;
import com.huaguoguo.cyg.entity.tl.to.Gang;
import com.huaguoguo.cyg.entity.tl.to.Player;
import com.huaguoguo.cyg.entity.tl.to.Trumpet;
import com.huaguoguo.cyg.service.tl.data.ITlGangService;
import com.huaguoguo.cyg.service.tl.data.ITlPlayerService;
import com.huaguoguo.cyg.service.tl.data.ITlTrumpetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类说明： tlrank爬到的数据进行处理<br/>
 * author:huaguoguo
 * Date: 2019/8/16
 * Time: 23:15
 */
@Slf4j
@Component
public class TLRankPipeline implements Pipeline {

    @Autowired
    private ITlGangService tlGangService;
    @Autowired
    private ITlPlayerService tlPlayerService;
    @Autowired
    private ITlTrumpetService tlTrumpetService;


    /**
     * 处理爬虫数据
     *
     * @param resultItems resultItems
     * @param task        task
     */
    @Override
    public void process(ResultItems resultItems, Task task) {
        String flag = (String) resultItems.get("flag");
        switch (flag) {
            case "gang":
                gangsPipeline(resultItems);
                break;
            case "player":
                playersPipeline(resultItems);
                break;
            case "trumpet":
                trumpetPipeline(resultItems);
                break;
            default:return;
        }
    }

    private void trumpetPipeline(ResultItems resultItems) {
        List<Trumpet> trumpets = (List<Trumpet>) resultItems.get("trumpets");
        List<TlTrumpetDTO> trumpetDTOList = trumpets.stream().map(trumpet -> {
            TlTrumpetDTO trumpetDTO = TlTrumpetDTO.builder().build();
            BeanUtil.copyProperties(trumpet, trumpetDTO);
            return trumpetDTO;
        }).collect(Collectors.toList());
        tlTrumpetService.saveBatch(trumpetDTOList);
    }

    public void gangsPipeline(ResultItems resultItems){
        List<Gang> gangs = (List<Gang>) resultItems.get("gangs");
        List<TlGangDTO> gangDTOList = gangs.stream().map(gang -> {
            TlGangDTO gangDTO = TlGangDTO.builder().build();
            BeanUtil.copyProperties(gang, gangDTO);
            return gangDTO;
        }).collect(Collectors.toList());
        tlGangService.saveBatch(gangDTOList);
    }

    public void playersPipeline(ResultItems resultItems){
        List<Player> players = (List<Player>) resultItems.get("players");
        List<TlPlayerDTO> playerDTOList = players.stream().map(player -> {
            TlPlayerDTO playerDTO = TlPlayerDTO.builder().build();
            BeanUtil.copyProperties(player, playerDTO);
            return playerDTO;
        }).collect(Collectors.toList());
        tlPlayerService.saveBatch(playerDTOList);
    }
}
